unit MtMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, MtThread;

type
  TfrmMtMain = class(TForm)
    btnStart: TButton;
    btnStop: TButton;
    mmoShow: TMemo;
    btnClear: TButton;
    edtInterval: TEdit;
    lbl1: TLabel;
    lbl2: TLabel;
    edtTimes: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure btnStartClick(Sender: TObject);
    procedure btnStopClick(Sender: TObject);
    procedure DoTimer(Sender: TObject);
    procedure DoTimerOver(Sender: TObject);
    procedure btnClearClick(Sender: TObject);
    procedure ChangeButtonState(AEnable: Boolean);
  private
    Ftmr: TMtTimer;
  public
    procedure Shown(AMsg: string);
  end;

var
  frmMtMain: TfrmMtMain;

implementation

{$R *.dfm}

procedure TfrmMtMain.btnClearClick(Sender: TObject);
begin
  mmoShow.Lines.Clear;
end;

procedure TfrmMtMain.btnStartClick(Sender: TObject);
var
  iExecTimes: Integer;
begin
  Ftmr := TMtTimer.Create('MtTimer');
  Ftmr.Interval := StrToIntDef(edtInterval.Text, 3);
  iExecTimes := StrToIntDef(edtTimes.Text, 0);
  Ftmr.LimitTimes := iExecTimes > 0;
  Ftmr.ExecTimes := iExecTimes;
  Ftmr.OnTimerEvent := DoTimer;
  Ftmr.OnTimerOverEvent := DoTimerOver;
  Ftmr.Enabled := True;
  ChangeButtonState(False);
end;

procedure TfrmMtMain.btnStopClick(Sender: TObject);
begin
  Ftmr.Enabled := False;
  Ftmr.Free;
  ChangeButtonState(True);
end;

procedure TfrmMtMain.ChangeButtonState(AEnable: Boolean);
begin
  btnStart.Enabled := AEnable;
  btnStop.Enabled := not AEnable;
end;

procedure TfrmMtMain.DoTimer(Sender: TObject);
begin
  Shown(IntToStr(Random(65535)));
end;

procedure TfrmMtMain.DoTimerOver(Sender: TObject);
begin
  ChangeButtonState(True);
end;

procedure TfrmMtMain.FormCreate(Sender: TObject);
begin
  mmoShow.Lines.Clear;
end;

procedure TfrmMtMain.Shown(AMsg: string);
begin
  mmoShow.Lines.Add(FormatDateTime('HH:nn:ss: ', Now) + AMsg);
end;

end.
